package com.igeek.ch02.dao;

import com.igeek.ch02.entity.Items;
import com.igeek.ch02.utils.JDBCUtilsByC3P0;

import java.sql.SQLException;
import java.util.List;

/**
 * @author chenmin
 * 2023/7/27 10:17
 * @description TODO
 */
public class ItemsDao extends BaseDao<Items> {

    //查询总记录数
    public int selectCounts(String query) throws SQLException {
        String sql = "select count(*) from items where name like concat('%',?,'%') and isDeleted = 0";
        Long o = (Long)this.selectSingleValue(JDBCUtilsByC3P0.getConn(), sql, query);
        return o.intValue();
    }

    //查询商品列表
    public List<Items> selectAllItems(String query , int begin) throws SQLException {
        String sql = "select * from items where name like concat('%',?,'%') and isDeleted = 0 limit ?,3";
        List<Items> items = this.selectAll(JDBCUtilsByC3P0.getConn(), sql, Items.class, query, begin);
        return items;
    }

    //通过ID编号查询商品信息
    public Items selectOneById(int id) throws SQLException {
        String sql = "select * from items where id = ?";
        Items items = this.selectOne(JDBCUtilsByC3P0.getConn(), sql, Items.class, id);
        return items;
    }

    //更新商品信息
    public int updateItems(Items items) throws SQLException {
        String sql = "update items set name = ? , detail = ? , price = ? , createtime = ? , pic = ? , isDeleted = ? where id = ?";
        int i = this.update(JDBCUtilsByC3P0.getConn(), sql, items.getName(), items.getDetail(),
                items.getPrice(), items.getCreatetime(), items.getPic(),
                items.getIsDeleted(), items.getId());
        return i;
    }
    public int addItems(Items items) throws SQLException {
        String sql = "insert into items values(?,?,?,?,?,?,?)";
        int i = this.update(JDBCUtilsByC3P0.getConn(),sql,items.getId(),items.getName(),items.getPrice(),items.getDetail(),items.getPic(),items.getCreatetime(),items.getIsDeleted());
        return i;
    }

}
